package com.silence.springboot.service.impl;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.silence.springboot.convert.ConfirmDiagnosisInfoConvert;
import com.silence.springboot.entity.ConfirmDiagnosisInfoEntity;
import com.silence.springboot.mapper.ConfirmDiagnosisInfoMapper;
import com.silence.springboot.service.ConfirmDiagnosisInfoService;
import com.silence.springboot.vo.input.PageConfirmDiagnosisInfoInput;
import com.silence.springboot.vo.input.SaveConfirmDiagnosisInfoInput;
import com.silence.springboot.vo.input.WarningStudentInput;
import com.silence.springboot.vo.output.PageConfirmDiagnosisInfoOutput;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import static com.silence.springboot.entity.table.ConfirmDiagnosisInfoEntityTableDef.CONFIRM_DIAGNOSIS_INFO_ENTITY;

/**
 * 确诊记录表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ConfirmDiagnosisInfoServiceImpl extends ServiceImpl<ConfirmDiagnosisInfoMapper, ConfirmDiagnosisInfoEntity> implements ConfirmDiagnosisInfoService {

    /**
     * 转换器
     */
    private final ConfirmDiagnosisInfoConvert confirmDiagnosisInfoConvert;

    /**
     * 分页查询
     *
     * @param input 入参
     * @return 分页结果
     */
    @Override
    public Page<PageConfirmDiagnosisInfoOutput> pageInfo(PageConfirmDiagnosisInfoInput input) {
        Page<ConfirmDiagnosisInfoEntity> page = Page.of(input.getPageNum(), input.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(CONFIRM_DIAGNOSIS_INFO_ENTITY.ALL_COLUMNS)
                .from(CONFIRM_DIAGNOSIS_INFO_ENTITY)
                .where(CONFIRM_DIAGNOSIS_INFO_ENTITY.NAME.like(input.getName(), StringUtils::isNotBlank))
                .where(CONFIRM_DIAGNOSIS_INFO_ENTITY.STUDENT_NUMBER.eq(input.getStudentNumber(), StringUtils::isNotBlank))
                .where(CONFIRM_DIAGNOSIS_INFO_ENTITY.PHONE.eq(input.getPhone(), StringUtils::isNotBlank))
                .where(CONFIRM_DIAGNOSIS_INFO_ENTITY.WARNING_CODE.eq(input.getWarningCode(), StringUtils::isNotBlank));
        return confirmDiagnosisInfoConvert.convertPageConfirmDiagnosisInfoOutput(page(page, queryWrapper));
    }

    /**
     * 风险预警
     *
     * @param input 入参
     */
    @Override
    public void warningStudent(WarningStudentInput input) {
        ConfirmDiagnosisInfoEntity updateEntity = new ConfirmDiagnosisInfoEntity();
        updateEntity.setId(Long.parseLong(input.getId()));
        updateEntity.setWarningCode("1");
        updateById(updateEntity);
    }

    /**
     * 保存确诊信息
     *
     * @param input 入参
     */
    @Override
    public void saveInfo(SaveConfirmDiagnosisInfoInput input) {
        save(confirmDiagnosisInfoConvert.convertSaveConfirmDiagnosisInfoInput(input));
    }

    /**
     * 检查学生是否预警
     *
     * @param studentNumber 学号
     * @return 是否预警
     */
    @Override
    public boolean checkStudentIsWarning(String studentNumber) {
        if (StringUtils.isBlank(studentNumber)) {
            return false;
        }
        return count(QueryWrapper.create()
                .from(CONFIRM_DIAGNOSIS_INFO_ENTITY)
                .where(CONFIRM_DIAGNOSIS_INFO_ENTITY.STUDENT_NUMBER.eq(studentNumber)
                        .and(CONFIRM_DIAGNOSIS_INFO_ENTITY.WARNING_CODE.eq("1")))) > 0L;
    }
}